package fr.ordre66.vlcs.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import fr.ordre66.vlcs.dto.Upload;


@Repository("uploadDao")
public class UploadDao implements IUploadDao {

	@PersistenceContext private EntityManager entityManager;
	
		@Override
		public void uploadMedia(Upload upload) {
			System.out.println("persisting Upload instance");
			try {
				entityManager.persist(upload);
				System.out.println("persist successful");
			} catch (RuntimeException re) {
				System.err.println("persist failed" + re);
			}
		}

		@Override
		public Upload getLastImage(String login) {
			System.out.println("getting the last image");
			try {
				Query query = entityManager.createQuery("select u from Upload u where u.login = '"+login+"' and u.type='img' order by u.dateOfCreation DESC limit 1");
				Upload upload = (Upload) query.getResultList().get(0);
				System.out.println("getting successful");
				return upload;
			} catch (RuntimeException re) {
				System.err.println("getting failed "+ re);
				return null;
			}
		}

		@Override
		public void removeOldImg(String login) {
			System.out.println("Remove all images");
			try{
				Query query = entityManager.createQuery("delete from Upload u where u.type='img' and u.login = '"+login+"'");
				int result = query.executeUpdate();
				System.out.println("Number img deleted: " + result);
			} catch (Exception re) {
				System.err.println("Remove media failed ");
				re.printStackTrace();
			}	
		}

		@Override
		public List<Upload> getAllVideos(String login) {
			System.out.println("getting all the videos, 4 max");
			try {
				System.err.println("TEST");
				Query query = entityManager.createQuery("select from Upload where login = '"+login+"' and type = 'video' order by dateOfCreation DESC");
				@SuppressWarnings("unchecked")
				List<Upload> videosList = (List<Upload>) query.getResultList();
				System.err.println("TEST : " + query.getResultList().size());
				System.out.println("videos number: " + videosList.size());
				System.out.println("getting successful");
				return videosList;
			} catch (RuntimeException re) {
				System.err.println("getting all videos failed "+ re);
				return null;
			}
		}

		@Override
		public Upload getVideo(long keyID) {
			System.out.println("getting the videos with keyID: " + keyID);
			try {
				Key k = KeyFactory.createKey(Upload.class.getName(), new Long(keyID));
				Query query = entityManager.createQuery("select from Upload where id = :key");
				query.setParameter("key", k.getId());
				Upload video = (Upload) query.getSingleResult();
				System.out.println("getting successful");
				return video;
			} catch (RuntimeException re) {
				System.err.println("getting failed "+ re);
				return null;
			}
		}
		
		@Override
		public void removeVideo(long keyID){			
			System.out.println("Remove video");
			try{
				Key k = KeyFactory.createKey(Upload.class.getName(), new Long(keyID));
				Query query = entityManager.createQuery("delete from Upload where id = :key");
				query.setParameter("key", k.getId());
				int result = query.executeUpdate();
				System.out.println("Number video deleted: " + result);
			} catch (Exception re) {
				System.err.println("Remove video failed ");
				re.printStackTrace();
			}	
		}
	
}
